﻿<template>
  <my-container v-loading="pageLoading">
    <!--顶部操作-->
    <template #header>
      <el-form class="ad-form-query" :inline="true" @submit.native.prevent>
        <el-form-item>
          <my-search :fields="fields" @click="onSearch" />
        </el-form-item>
        <el-form-item v-if="checkPermission(['api:admin:wechatapp:add'])">
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
        <el-form-item v-if="checkPermission(['api:admin:wechatapp:delete'])">
          <my-confirm-button :disabled="sels.length === 0" :type="'delete'" :placement="'bottom-end'"
            :loading="deleteLoading" style="margin-left: 0px;" @click="onBatchDelete">
            <template #content>
              <p>确定要批量删除吗？</p>
            </template>
            批量删除
          </my-confirm-button>
        </el-form-item>
      </el-form>
    </template>

    <!--列表-->
    <el-table v-loading="listLoading" :data="pageList" highlight-current-row height="'100%'"
      style="width: 100%;height:100%;" @selection-change="onSelsChange">
           
      <el-table-column type="selection" width="50" />
      <!-- <el-table-column prop="weChatComponentId" label="微信组件Id" /> -->
      <el-table-column prop="openAppIdOrName" label="开放平台Id或名称" />

      <el-table-column prop="name" label="名称" />
       
      <el-table-column prop="typeName" label="微信app类型" />
           
      <el-table-column prop="displayName" label="显示名称" />
           
      <el-table-column prop="appId" label="微信AppId" />
           
      <el-table-column prop="appSecret" label="App密钥" />
           
      <el-table-column prop="token" label="微信Token" />
           
      <el-table-column prop="encodingAesKey" label="微信密钥" />

      <el-table-column v-if="checkPermission(['api:admin:wechatapp:update','api:admin:wechatapp:delete'])" label="操作"
        width="180">
                <template #default="{ $index, row }">
                    <el-button v-if="checkPermission(['api:admin:wechatapp:update'])" @click="onEdit($index, row)">编辑
          </el-button>
                   
          <my-confirm-button v-if="checkPermission(['api:admin:wechatapp:delete'])" type="delete"
            :loading="row._loading" @click="onDelete($index, row)" />
                 
        </template>
              </el-table-column>
    </el-table>

    <!--分页-->
    <template #footer>
      <my-pagination ref="pager" :total="total" :checked-count="sels.length" @get-page="pageList" />
    </template>

    <!-- 添加 -->
    <my-window v-if="checkPermission(['api:admin:wechatapp:add'])" title="添加" :visible.sync="addDialogFormVisible"
      @close="closeAddForm">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-width="120px" :inline="false">
        <el-form-item label="微信app类型" prop="type">
          <el-select v-model="addForm.type" placeholder="请选择微信app类型" style="width: 100%">
            <el-option v-for="item in typeList" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
             
        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="显示名称" prop="displayName">
          <el-input v-model="addForm.displayName" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="开放Id或者名称" prop="openAppIdOrName">
          <el-input v-model="addForm.openAppIdOrName" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="微信AppId" prop="appId">
          <el-input v-model="addForm.appId" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="App密钥" prop="appSecret">
          <el-input v-model="addForm.appSecret" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="微信Token" prop="token">
          <el-input v-model="addForm.token" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="微信密钥" prop="encodingAesKey">
          <el-input v-model="addForm.encodingAesKey" auto-complete="off" />
        </el-form-item>

      </el-form>
      <template #footer>
        <el-button @click.native="addDialogFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="addFormvalidate" :loading="addLoading" @click="onAddSubmit" />
      </template>
    </my-window>

    <!--编辑窗口-->
    <my-window v-if="checkPermission(['api:admin:wechatapp:update'])" title="编辑" :visible.sync="editFormVisible"
      @close="closeEditForm">
      <el-form ref="editForm" :model="editForm" :rules="editFormRules" label-width="120px" :inline="false">
        <el-form-item label="微信app类型" prop="type">
          <el-select v-model="editForm.type" placeholder="请选择微信app类型" style="width: 100%">
            <el-option v-for="item in typeList" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
             
        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="显示名称" prop="displayName">
          <el-input v-model="editForm.displayName" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="开放Id或者名称" prop="openAppIdOrName">
          <el-input v-model="editForm.openAppIdOrName" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="微信AppId" prop="appId">
          <el-input v-model="editForm.appId" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="App密钥" prop="appSecret">
          <el-input v-model="editForm.appSecret" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="微信Token" prop="token">
          <el-input v-model="editForm.token" auto-complete="off" />
        </el-form-item>
             
        <el-form-item label="微信密钥" prop="encodingAesKey">
          <el-input v-model="editForm.encodingAesKey" auto-complete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <my-confirm-button type="submit" :validate="editFormvalidate" :loading="editLoading" @click="onEditSubmit" />
      </template>
    </my-window>

  </my-container>
</template>

<script>
import {  formatTime, listToTree, getTreeParents } from '@/utils'
import { getPageList, get, add, update, softDelete,batchSoftDelete } from '@/api/wecha/we-chat-app'
import MyContainer from '@/components/my-container'
import MyConfirmButton from '@/components/my-confirm-button'
import MySearch from '@/components/my-search'
import MySearchWindow from '@/components/my-search-window'
import MyWindow from '@/components/my-window'

export default {
  name: 'wechatapp',
  components: { MyContainer, MyConfirmButton, MySearch, MySearchWindow, MyWindow },
  data() {
    return {
      // 高级查询字段
      fields: [
        { value: 'creationTime', label: '创建时间', type: 'date', operator: 'daterange',
          config: { type: 'daterange', format: 'yyyy-MM-dd', valueFormat: 'yyyy-MM-dd' }
        }
      ],
      dynamicFilter: null,
      pageList: [],
      typeList:[{
        name:"小程序",
        value:10
      },
      {
        name:"公众号",
        value:20
      },
      {
        name:"企业微信",
        value:30
      },{
        name:"开发平台",
        value:40
      }],
      total: 0,
      sels: [], // 列表选中列
      listLoading: false,
      pageLoading: false,
      editLoading: false,
      addLoading: false,
      deleteLoading: false,
      // 添加界面
      addDialogFormVisible: false,
      // 编辑界面是否显示
      editFormVisible: false, 
      editFormRules: {
         type: [{ required: true, message: '请输入微信app类型', trigger: 'blur' }],
         name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
         displayName: [{ required: true, message: '请输入显示名称', trigger: 'blur' }],
        //  openAppIdOrName: [{ required: true, message: '请输入开放平台Id或名称', trigger: 'blur' }],
         appId: [{ required: true, message: '请输入微信AppId', trigger: 'blur' }],
         appSecret: [{ required: true, message: '请输入App密钥', trigger: 'blur' }],
         token: [{ required: true, message: '请输入微信Token', trigger: 'blur' }],
         encodingAesKey: [{ required: true, message: '请输入微信密钥', trigger: 'blur' }],
         id: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      addFormRules: {
         type: [{ required: true, message: '请输入微信app类型', trigger: 'blur' }],
         name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
         displayName: [{ required: true, message: '请输入显示名称', trigger: 'blur' }],
        //  openAppIdOrName: [{ required: true, message: '请输入开放平台Id或名称', trigger: 'blur' }],
         appId: [{ required: true, message: '请输入微信AppId', trigger: 'blur' }],
         appSecret: [{ required: true, message: '请输入App密钥', trigger: 'blur' }],
         token: [{ required: true, message: '请输入微信Token', trigger: 'blur' }],
         encodingAesKey: [{ required: true, message: '请输入微信密钥', trigger: 'blur' }],
      },
      // 编辑界面数据
      editForm: {
        id: "",
        type: "",
        name: "",
        displayName: "",
        openAppIdOrName: "",
        appId: "",
        appSecret: "",
        token: "",
        encodingAesKey: "",
        id: "",
      },
      // 添加界面数据
      addForm: {
        type: "",
        name: "",
        displayName: "",
        openAppIdOrName: "",
        appId: "",
        appSecret: "",
        token: "",
        encodingAesKey: "",
      }
    }
  },
  async mounted() {
    await this.onGetPageList()
  },
  methods: {
    formatCreatedTime(row, column, time) {
      return formatTime(time, 'YYYY-MM-DD HH:mm')
    },
    // 查询
    onSearch(dynamicFilter) {
      this.$refs.pager.setPage(1)
      this.dynamicFilter = dynamicFilter
      this.onGetPageList()
    },
    // 显示添加界面
    onAdd() {
      this.addDialogFormVisible = true;
    },
    // 关闭添加
    closeAddForm() {
      this.$refs.addForm.resetFields()
      this.addDialogFormVisible = false
    },
    // 关闭编辑
    closeEditForm() {
      this.$refs.editForm.resetFields()
      this.editFormVisible = false
    },
    // 添加验证
    addFormvalidate() {
      let isValid = false
      this.$refs.addForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 编辑验证
    editFormvalidate() {
      let isValid = false
      this.$refs.editForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 删除验证
    deleteValidate(row) {
      let isValid = true
      return isValid
    },
    // 批量删除验证
    batchDeleteValidate() {
      let isValid = true
      return isValid
    },
    // 选择
    onSelsChange(sels) {
      this.sels = sels
    },
    // 获取列表
    async onGetPageList() {
      var pager = this.$refs.pager.getPager()
      const params = {
        ...pager,
        dynamicFilter: this.dynamicFilter
      }
      this.listLoading = true
      const res = await getPageList(params)
      this.listLoading = false
      if (!res?.success) {
        return
      }
      this.total = res.data.total
      const data = res.data.list
      data.forEach(d => {
        d._loading = false
      })
      this.pageList = data
    },
    // 显示编辑界面
    async onEdit(index, row) {
      this.pageLoading = true
      const res = await get({ id: row.id })
      this.pageLoading = false
      if (res && res.success) {
        const data = res.data;
        this.editForm = data;
        this.editFormVisible = true;
      }
    },
    // 添加
    async onAddSubmit() {
      this.addLoading = true;
      const para = _.cloneDeep(this.addForm);
      const res = await add(para);
      this.addLoading = false;

      if (!res?.success) {
        return;
      }
      this.$message({
        message: this.$t("admin.addOk"),
        type: "success",
      });
      this.$refs["addForm"].resetFields();
      this.addDialogFormVisible = false;
      this.onGetPageList();
    },
    // 编辑
    async onEditSubmit() {
      this.editLoading = true
      const para = _.cloneDeep(this.editForm)
      const res = await update(para)
      this.editLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.updateOk'),
        type: 'success'
      })
      this.$refs['editForm'].resetFields()
      this.editFormVisible = false
      this.onGetPageList()
    },
    // 批量删除
    async onBatchDelete() {
      const para = { ids: [] };
      para.ids = this.sels.map((s) => {
        return s.id;
      });
      this.deleteLoading = true;
      const res = await batchSoftDelete(para.ids);
      this.deleteLoading = false;

      if (!res?.success) {
        return;
      }

      this.$message({
        message: this.$t("admin.batchDeleteOk"),
        type: "success",
      });

      this.onGetPageList();
    },
    // 删除
    async onDelete(index, row) {
      row._loading = true
      const para = { id: row.id }
      const res = await softDelete(para)
      row._loading = false
      if (!res?.success) {
        return
      }
      this.$message({
        message: this.$t('admin.deleteOk'),
        type: 'success'
      })
      this.onGetPageList()
    }
  }
}
</script>

<style lang="scss" scoped>
.my-search ::v-deep .el-input-group__prepend {
  background-color: #fff;
}
</style>
